

def coroutine(func):
    def start(*args, **kwargs):
        cr = func(*args, **kwargs)
        next(cr)
        return cr
    return start

@coroutine
def grep(pattern):
    print("Looking for {}".format(pattern))
    try:
        while True:
            line = (yield)
            if pattern in line:
                print(line)
    except GeneratorExit:
        print('Going away!')


def main(*args, **kwargs):
    gp = grep('python')
    gp.send('haha python')
    gp.send('haha python')
    gp.send('haha python')
    gp.send('haha')
    gp.close()

if __name__ == '__main__':
    main()
